/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dao;

import com.entity.Classes;

import com.entity.Teacher;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author DELL
 */
public class TeacherDao {
    
    
        private Session session;
    private Transaction transaction;
    private Query query;

    private SessionFactory sessionFactory = null;

    private HibernateTemplate ht = null;

    public void setSessionFactory(SessionFactory sessionFactory) {

        this.sessionFactory = sessionFactory;
    }

    private HibernateTemplate getHibernateTemplate() {
        if (ht == null) {

            ht = new HibernateTemplate(sessionFactory);
        }
        return ht;
    }

    public void saveTeacher(Teacher tc) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        try {

            transaction = session.beginTransaction();
             Logger.getLogger(this.getClass().getName()).log(Level.INFO,"cccccccccccccccccccccccccccccc");
            session.save(tc);
             Logger.getLogger(this.getClass().getName()).log(Level.INFO,"cdddddddddddddddddddddddddddddc");
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }

    public void updateTeacher(Teacher tc) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        try {

            transaction = session.beginTransaction();
            session.update(tc);
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }

    public void deleteTeacher(int tid ) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        try {

            transaction = session.beginTransaction();
            session.delete(session.get(Teacher.class, tid));
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }

    public Teacher getTeacher(int tid) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        Teacher teacher = (Teacher) session.get(Teacher.class, tid);
        session.close();
        return teacher;
    }

    public List<Teacher> allTeacher(int pageNumber) {
        List<Teacher> allTeachers = new ArrayList<Teacher>();
        String hqlString = "from Teacher as st";

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        query = session.createQuery(hqlString);
        query.setFirstResult((pageNumber - 1) * 10);
        query.setMaxResults(10);
        allTeachers = query.list();
        session.close();
        return allTeachers;

    }

  

    public Teacher getTeacher1(String m) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        Query query2 = (Query) session.createQuery("from Teacher as t where teaId='" + m + "'");

        Teacher tc = (Teacher) query2.uniqueResult();
        session.close();
        return tc;

    }

    public boolean tcLogin(Teacher tc) {

        if (tc.getTeaId() != null && tc.getTpassword() != null) {
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "ttttttttt55555555555555555555555555555543331");
            
           
            
            
            String hqlString = "from Teacher as t where teaId='" + tc.getTeaId() + "' and  tpassword='" +  tc.getTpassword() + "'";
            Teacher t = null;
            ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
            SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
            Session session = sessionFactory.openSession();
            Query query = (Query) session.createQuery("from Teacher as t where teaId='" + tc.getTeaId() + "' and  tpassword='" +  tc.getTpassword() + "'");
            t = (Teacher) query.uniqueResult();

            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "ttttttttt4444444444444444444444444443331");

            if (t != null) {
                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "ttttttttt4444444444444444444444444443331");
                session.close();
                return true;
            }

        }
        return false;

        
    }

  

    public int getTeacherAmount() {
        int teacherAmount = 0;

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        String hqlString = "select count(*) from Teacher as tc";
        query = session.createQuery(hqlString);
        long count = (Long) query.uniqueResult();
        teacherAmount = (int) count;

        session.close();
        return teacherAmount;
    }
    
    
    
    

    
    
    
    
    
    
    
    
    
}
